local skel = fk.CreateSkill {
  name = "emo__haixing",
}

Fk:loadTranslationTable{
  ["emo__haixing"] = "骇型",
  [":emo__haixing"] = "每回合限一次，你弃置牌后，哈斯塔可以无视距离地使用其中一张。",

  ["#emo__haixing-use"] = "骇型：你可以使用一张 %src 弃置的牌！",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name) == 0 then
      if not table.find(player.room.alive_players, function (p)
        return p.general == "emo__hastur" or p.deputyGeneral == "emo__hastur"
      end) then return false end
      local ids = {}
      for _, move in ipairs(data) do
        if move.from == player and move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if player.room:getCardArea(info.cardId) == Card.DiscardPile and not Fk:getCardById(info.cardId).is_passive then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end
      if #ids > 0 then
        event:setCostData(self, {cards = ids})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards ---@type integer[]
    for _, p in ipairs(room:getAlivePlayers()) do
      if p.general == "emo__hastur" or p.deputyGeneral == "emo__hastur" then
        local use = room:askToUseRealCard(p, {
          pattern = cards, skill_name = skel.name, expand_pile = cards, skip = true,
          prompt = "#emo__haixing-use:" .. player.id,
          extra_data = {
            bypass_distances = true, bypass_times = true, expand_pile = cards,
          }
        })
        if use then
          event:setCostData(self, {use = use, cards = cards})
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player)
    local room = player.room
    local use = event:getCostData(self).use
    room:useCard(use)
  end,
})

return skel
